home *** CD-ROM | disk | FTP | other *** search
- Path: quadostimpy.natinst.com!user
- From: rcauvin@natinst.com (Roger L. Cauvin)
- Newsgroups: comp.lang.c++
- Subject: Re: Dynamic Casting / RTTI
- Date: 20 Mar 1996 15:56:03 GMT
- Organization: National Instruments
- Message-ID: <rcauvin-2003961001170001@quadostimpy.natinst.com>
- References: <DoJF68.Mx8@news.uwindsor.ca>
- NNTP-Posting-Host: quadostimpy.natinst.com
-
- In article <DoJF68.Mx8@news.uwindsor.ca>, saed@engn.uwindsor.ca wrote:
-
- > Hi All,
- >
- > given the following code:
- >
- > class Base{};
- > class Deri : public Base{};
- > main(){ Base * pB = new Deri; };
- >
- > --- How do can I obtain a 'Deri' class pointer to the same object as pB?
- >
- > sort of like: Deri * pD = dynamic_cast<Deri*>(pB);
- > but this yields an error:
- > cannot dynamic_cast `pB' (of type `class Base *') to type `class Deri *'
-
- Two things:
-
- 1. A dynamic cast is not needed in the example given above. You could
- use a static cast instead without any compiler complaints.
-
- 2. In order for a dynamic cast to work, the base class must have at least
- one virtual function. The virtual table is used to store the runtime type
- information, and if one is not present, then no RTTI exists either.
-
-
- Roger
-
- ---
-
- Roger L. Cauvin
- rcauvin@natinst.com
- Software Engineer
- National Instruments
-